﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyVBAScript
{
    public partial class dlgEditScript : Form
    {
        

        public dlgEditScript()
        {
            InitializeComponent();
            this.DialogResult = DialogResult.Cancel;
        }

        private XVBAEngine myVBAEngine = null;

        /// <summary>
        /// 脚本方法
        /// </summary>
        public XVBAEngine VBAEngine
        {
            get { return myVBAEngine; }
            set { myVBAEngine = value; }
        }

        private void frmEditScript_Load(object sender, EventArgs e)
        {
            this.txtScript.Text = myVBAEngine.ScriptText;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Filter = "VB脚本文件(*.vbs)|*.vbs";
                dlg.CheckFileExists = true;
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    System.IO.StreamReader reader = new System.IO.StreamReader(dlg.FileName, System.Text.Encoding.GetEncoding("gb2312"));
                    txtScript.Text = reader.ReadToEnd();
                    reader.Close();
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog dlg = new SaveFileDialog())
            {
                dlg.Filter = "VB脚本文件(*.vbs)|*.vbs";
                dlg.CheckPathExists = true;
                dlg.OverwritePrompt = true;
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    System.IO.StreamWriter writer = new System.IO.StreamWriter(dlg.FileName, false, System.Text.Encoding.GetEncoding("gb2312"));
                    writer.Write(txtScript.Text);
                    writer.Close();
                }
            }
        }

        private void cmdCompile_Click(object sender, EventArgs e)
        {
            myVBAEngine.ScriptText = txtScript.Text;
            if (myVBAEngine.Compile() == false)
            {
                MessageBox.Show(this, "编译错误:" + myVBAEngine.CompilerOutput, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                MessageBox.Show(this, "编译成功", "系统提示");
            }
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            myVBAEngine.ScriptText = txtScript.Text;
            myVBAEngine.CheckReady();
            this.DialogResult = DialogResult.OK;
            this.Close();

        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}